package com.clouddot.baipai.util.file;

/**
 * Created by wxy on 2018/4/2.
 */


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class fileUtil {
    private static fileUtil instance;

    public static fileUtil getInstance() {
        if (instance == null) {
            instance = new fileUtil();
        }
        return instance;
    }

    public String createIfNotExist(String path, String name) {

        File mkFile = new File(path);
        if (!mkFile.exists()) {
            mkFile.mkdirs();
        }

        path += name;
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }


    public boolean writeBytes(String filePath, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(data);
            fos.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }


    public byte[] readBytes(String file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            fis.read(buffer);
            fis.close();
            return buffer;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

    public void writeString(String file, String content) {
        try {
            FileWriter writer = new FileWriter(file, true);
            writer.write(content);
            writer.write("\r\n");
            writer.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public void writeStringNotAppend(String file, String content) {
        try {
            FileWriter writer = new FileWriter(file, false);
            writer.write(content);
            writer.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public String readString(String file, String charset) {
        byte[] data = readBytes(file);
        String ret = null;

        try {
            ret = new String(data, charset);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }

    public String createFileName() {
        return "/" + getCurrentDateString() + ".txt";
    }

    /**
     * 打印错误日志
     *
     * @param ex
     */
    public String Errorinfo(Throwable ex) {
        String info = null;
        ByteArrayOutputStream baos = null;
        PrintStream printStream = null;
        try {
            baos = new ByteArrayOutputStream();
            printStream = new PrintStream(baos);
            ex.printStackTrace(printStream);
            byte[] data = baos.toByteArray();
            info = new String(data);
            data = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (printStream != null) {
                    printStream.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return getCurrentDateString4Second() + "\n" + info;
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    private static String getCurrentDateString() {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
                Locale.getDefault());
        Date nowDate = new Date();
        result = sdf.format(nowDate);
        return result;
    }

    private static String getCurrentDateString4Second() {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault());
        Date nowDate = new Date();
        result = sdf.format(nowDate);
        return result;
    }

    public static String getCurrentTimeString() {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH",
                Locale.getDefault());
        Date nowDate = new Date();
        result = sdf.format(nowDate);
        return result;
    }
}